<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\wxapp\controller;

use api\wxapp\controller\ApiBaseController;
use think\Db;
use api\wxapp\model\Comment;
use api\wxapp\model\CommentReply;
class CommentController extends ApiBaseController
{
    public function index(Comment $comment)
    {
        $params = $this->getParam();
        $this->checkgoodsId($params);
        $list = $comment->getList($params['goods_id']);
        $this->success('请求成功!',$list);

    }
    //添加评论
    public function add()
    {
        $params = $this->getParam();
        $this->checkuid($params);
        $this->checkgoodsId($params);
        $this->checkcontent($params);
        $res = Comment::insert($params);
        if($res){
            $this->success('请求成功!',true);
        }else{
            $this->error('失败!',false);
        }
    }

    //店主回复
    public function reply()
    {
        $params = $this->getParam();
        $this->checkcontent($params);
        !isset($params['comment_id']) ? $this->error('参数缺失!!!') : '';
        $params['addtime'] = $this->returnDateTime();
        $res = CommentReply::insert($params);
        if($res){
            $this->success('请求成功!',true);
        }else{
            $this->error('失败!',false);
        }

    }
    //商品评分
    public function score(Comment $comment)
    {
        $params = $this->getParam();
        $this->checkgoodsId($params);
        $list = $comment->getScore($params['goods_id']);
        $this->success('请求成功!',$list);
    }
    //
    public function scoreAdd()
    {
        $params = $this->getParam();
        $this->checkuid($params);
        $this->checkgoodsId($params);
        !isset($params['score']) ? $this->error('参数缺失!!!!!') : '';
        $find = db::name('app_goods_score')->where(['uid'=>$params['uid'],'goods_id'=>$params['goods_id']])->find();
        if($find){
            //已经评过分 修改
            $res = db::name('app_goods_score')->where('id',$find['id'])->update(['score'=>$params['score']]);
        }else{
            $res = db::name('app_goods_score')->insert($params);
        }
        if($res){
            $this->success('请求成功');
        }else{
            $this->error('请求失败');
        }
    }
    //我的评论
    public function user()
    {
        $params = $this->getParam();
        $this->checkuid($params);
        $list = Comment::where('uid',$params['uid'])->select();
        foreach($list as $k=>$v){
            $list[$k]['img'] = json_decode($v['img']);
        }
        $this->success('请求成功!',$list);
    }
    //删除评论
    public function delete()
    {
        $params = $this->getParam();
        $this->checkid($params);
        $res = Comment::destroy($params['id']);
        //同事删除评论的回复
        CommentReply::where('comment_id',$params['id'])->delete();
        if($res){
            $this->success('请求成功!');
        }else{
            $this->error('请求失败!!!!');
        }
    }
}
